package com.sgb.contract.view.ui.approval;

import android.content.Intent;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivityApprovalBinding;
import com.sgb.contract.model.ContractApprovalReBean;
import com.sgb.contract.model.ContractDetailBean;
import com.sgb.contract.model.ContractOrganizationBean;
import com.sgb.contract.model.OrganizationDepartmentBean;
import com.sgb.contract.utils.ArouterConfigs;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 描述:合同-审批申请
 *
 * @author qiangHeNoo
 * <p>
 * 时间:2021.11.03
 */
@Route(path = ArouterConfigs.AR_CONTRACT_APPROVAL)
public class ApprovalActivity extends BaseActivity<ContractActivityApprovalBinding, ApprovalModel> {

    private final AtomicInteger selectedPosition = new AtomicInteger();
    /**
     * 审批申请请求实体
     */
    private final ContractApprovalReBean contractApprovalReBean = new ContractApprovalReBean();

    private ContractDetailBean contractDetailBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_approval;
    }

    @Override
    protected void init() {
        initView();
        loadData();
    }

    private void initView() {
        mDataBinding.head.title.setText("审批申请");
    }

    private void loadData() {
        Intent intent = getIntent();
        if (intent != null) {
            this.contractDetailBean = (ContractDetailBean) intent.getSerializableExtra("CONTRACT_DETAIL");
            if (contractDetailBean != null) {
                contractApprovalReBean.setId(contractDetailBean.id);
            }
            //当前只有签署合同需要审批申请
            contractApprovalReBean.setType("signContract");
        }
        mViewModel.getBusinessInfoByCompany();
    }

    @Override
    public void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.contractLlSelect.setOnClickListener(v -> {
            mViewModel.getOrganizationDepartment();
        });

        mDataBinding.contractApplication.setOnClickListener(v -> {
            if (contractDetailBean != null) {
                mViewModel.checkStatusByNowStatus(contractDetailBean);
            }
        });
    }

    @Override
    protected void onDialogOnClick(View dialogView) {
        if (null != dialogView.getTag() && "sendContractApprovalProcess".equals(dialogView.getTag().toString())) {
            finish();
        } else if (null != dialogView.getTag() && "checkApprovalOpenAudit".equals(dialogView.getTag().toString())) {
            finish();
        }
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ApprovalModel.class, BR.approvalModel);
    }

    @Override
    public void initObserve() {
        mViewModel.showCompanyToastMessage.observe(this, aBoolean -> {
            MToast.showToast(this, "服务器异常~");
        });
        mViewModel.showDepartmentToastMessage.observe(this, aBoolean -> {
            MToast.showToast(this, "获取部门信息失败~");
        });
        mViewModel.showDepartmentToastMessage1.observe(this, aBoolean -> {
            contractApprovalReBean.setRemark(mDataBinding.contractContent.getText().toString().trim());
            mViewModel.sendContractApprovalProcess(contractApprovalReBean);
        });
        mViewModel.businessInfoByCompanyBeanMutableLiveData.observe(this, businessInfoByCompanyBean -> {
            mDataBinding.contractCompanyName.setText(businessInfoByCompanyBean.getCompanyName());
            mDataBinding.contractName.setText(businessInfoByCompanyBean.getRealName());

            contractApprovalReBean.setCompanyName(businessInfoByCompanyBean.getCompanyName());
            contractApprovalReBean.setCompanyNo(businessInfoByCompanyBean.getNo());
            contractApprovalReBean.setApplyName(businessInfoByCompanyBean.getRealName());

        });
        mViewModel.departmentBeanList.observe(this, departmentBeans -> {
            List<OrganizationDepartmentBean> organizationDepartmentBeans = new ArrayList<>();
            for (ContractOrganizationBean contractOrganizationBeanList : departmentBeans) {
                List<ContractOrganizationBean.ChildrenDTO> children = contractOrganizationBeanList.children;
                if (children != null && children.size() != 0) {
                    for (ContractOrganizationBean.ChildrenDTO childrenDTO : children) {
                        OrganizationDepartmentBean organizationDepartmentBean = new OrganizationDepartmentBean();
                        organizationDepartmentBean.setNo(childrenDTO.id);
                        organizationDepartmentBean.setName(childrenDTO.name);
                        organizationDepartmentBeans.add(organizationDepartmentBean);
                    }
                }

            }
            if (organizationDepartmentBeans.size() == 0) {
                MToast.showToast(this, "未获取到相关部门信息~");
                return;
            }

            OptionsPickerView businessOption = new OptionsPickerBuilder(ApprovalActivity.this, (options1, options2, options3, v1) -> {
                String deptName = organizationDepartmentBeans.get(options1).getName();
                mDataBinding.contractDepartment.setText(deptName);
                mDataBinding.contractDepartment.setTextColor(ContextCompat.getColor(this, R.color.color_ff000000));
                selectedPosition.set(options1);
                contractApprovalReBean.setDeptName(deptName);
                contractApprovalReBean.setDeptNo(organizationDepartmentBeans.get(options1).getNo());
            }).setLineSpacingMultiplier(2.2f)
                    .setCancelColor(0xFF666666)
                    .setTitleColor(0xFF969696)
                    .setSubCalSize(15)
                    .setTitleSize(15)
                    .setContentTextSize(15)
                    .build();
            businessOption.setPicker(organizationDepartmentBeans);
            businessOption.setSelectOptions(selectedPosition.get());
            businessOption.setTitleText("申请部门");
            businessOption.show();
        });

    }

}